package com.lly.tboc.zhouyi

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBackIosNew
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.SearchBar
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.SuggestionChip
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.isTraversalGroup
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.traversalIndex
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.lly.tboc.zhouyi.vm.GuaViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun GuaSearchScreen(
    keyword: String,
    onDismissRequest: () -> Unit,
    scrollToGua: (Int) -> Unit,
    viewModel: GuaViewModel = hiltViewModel(),
) {
    val expanded = true
    val query = rememberSaveable(keyword) {
        mutableStateOf(keyword)
    }
    Box(
        Modifier
            .fillMaxSize()
            .semantics { isTraversalGroup = true }) {
        SearchBar(
            modifier = Modifier
                .align(Alignment.TopCenter)
                .semantics { traversalIndex = 0f },
            inputField = {
                SearchBarDefaults.InputField(
                    query = query.value,
                    onQueryChange = { query.value = it },
                    onSearch = {},
                    expanded = expanded,
                    onExpandedChange = {
                        if (!it) {
                            onDismissRequest()
                        }
                    },
                    placeholder = { Text("Hinted search text") },
                    leadingIcon = {
                        Row(verticalAlignment = Alignment.CenterVertically) {
                            IconButton(onClick = onDismissRequest) {
                                Icon(Icons.Default.ArrowBackIosNew, contentDescription = "back")
                            }
                            Icon(
                                Icons.Default.Search,
                                contentDescription = "search",
                            )
                        }
                    },
                    trailingIcon = {
                        IconButton(onClick = {
                            query.value = ""
                        }) {
                            Icon(Icons.Default.Clear, contentDescription = "clear")
                        }
                    },
                )
            },
            expanded = expanded,
            onExpandedChange = {
                if (!it) {
                    onDismissRequest()
                }
            },
        ) {
            val results by viewModel.search(query.value)
                .collectAsStateWithLifecycle(initialValue = emptyList())
            LazyColumn(Modifier.padding(16.dp)) {
                items(items = results, key = { it.id }) { gua ->
                    SuggestionChip(
                        modifier = Modifier.fillMaxWidth(),
                        onClick = {
                            scrollToGua(gua.id)
                        },
                        label = { Text(text = "${gua.name}:${gua.ci}") })
                }
            }
        }
    }

}